from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
from rest_framework.response import Response
from Util.Return import get_next_available_name
from Util.SQL import *


@csrf_exempt
@api_view(['GET', 'POST'])
def get_dataset(request):
    sql_string = "SELECT * FROM dataset;"
    dataset = select(sql_string)
    return Response(data=dataset)


@csrf_exempt
@api_view(['GET', 'POST'])
def change_dataset(request):
    data_dict = request.data
    update_data = {"name": data_dict['name'], "attribute": data_dict['attribute'], "available": data_dict['available']}
    condition = f"dataset_id = '{data_dict['dataset_id']}'"
    status = update(update_data, condition, "dataset")
    return Response(data=status)


@csrf_exempt
@api_view(['GET', 'POST'])
def del_dataset(request):
    data_dict = request.data
    condition = f"dataset_id = '{data_dict['dataset_id']}'"
    table = "dataset"
    result = delete(condition, table)
    return Response(data=result)
